package test01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SendUDP {
	public static final byte[] DEST_ADDR = new byte[]{(byte) 158, (byte) 130, (byte) 92, (byte) 168};
	public static final int DEST_PORT = 4505;
	
	public static void main(String str[]) {
		//getNameAndIP();
		//sendUDP();
		try {
			socket01();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void socket01()throws UnknownHostException, IOException{
		String machine = "18.145.0.30";
		final int daytimeport = 13;
		Socket so = new Socket(machine, daytimeport);
		BufferedReader br = new BufferedReader(new InputStreamReader(so.getInputStream()));
		String timestamp = br.readLine();
		System.out.println(machine + " says it is " + timestamp);
}
	
	public static void getNameAndIP() {
		InetAddress address, destAddr;
		try {
			address = InetAddress.getLocalHost();
			System.out.println("===SendUDP===");
			System.out.println("Local Host: " + address.getHostName() + ", " + address.getHostAddress());
			destAddr = InetAddress.getByAddress(DEST_ADDR);
			System.out.println("Dest Addr: " + destAddr.getHostName() + ", " + destAddr.getHostAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendUDP(){
		try {
			InetAddress destAddr = InetAddress.getByAddress(DEST_ADDR);
			byte[] testBytes = new byte[]{(byte) 1, (byte) 2, (byte) 3, (byte) 99};
			DatagramPacket message = new DatagramPacket(testBytes, testBytes.length, destAddr, DEST_PORT);
			DatagramSocket sock = new DatagramSocket();
			sock.send(message);
			sock.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
